package com.me.modules.sys.service.impl;

import com.me.modules.sys.dao.*;
import com.me.modules.sys.entity.GroupEntity;
import com.me.modules.sys.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date : 2018/12/7 16:24
 * @Author : zhenglei
 * @Desc : 工作组
 */
@Service("groupService")
public class GroupServiceImpl implements GroupService {

    @Autowired
    private GroupDao groupDao;

    @Autowired
    private OperationGroupDao operationGroupDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private GroupUserDao groupUserDao;
    @Autowired
    private GroupRoleDao groupRoleDao;

    @Override
    public GroupEntity queryObject(Integer id) {
        return groupDao.queryObject(id);
    }

    /**
     * 查询工作组列表
     */
    @Override
//    @CacheEvict(value = "group", allEntries = true)
    public List<GroupEntity> queryList(Map<String, Object> map) {
        return groupDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return groupDao.queryTotal(map);
    }

    @Override
    public void save(GroupEntity group) {
        groupDao.save(group);
        saveGroupRelateOperation(group);
        saveGroupRelateUser(group);
        saveGroupRelateRole(group);
    }

    @Override
    public void update(GroupEntity group) {
        groupDao.update(group);
        saveGroupRelateOperation(group);
        saveGroupRelateUser(group);
        saveGroupRelateRole(group);
    }
    /**
     * 保存用户关联角色
     * @param group
     */
    private void saveGroupRelateRole(GroupEntity group) {
        Map<String,Object> map = new HashMap<>();
        map.put("groupId",group.getId());
        map.put("roleIdList",group.getRoleIdList());
        groupRoleDao.delete(group.getId());
        if(group.getRoleIdList() != null && group.getRoleIdList().size() > 0){
            groupRoleDao.save(map);
        }
    }
    /**
     * 保存组内用户
     * @param group
     */
    private void saveGroupRelateUser(GroupEntity group) {
        Map<String,Object> map = new HashMap<>();
        map.put("groupId",group.getId());
        map.put("userIdList",group.getUserIdList());
        groupUserDao.deleteGroupAllUser(group.getId());
        if(group.getUserIdList() != null && group.getUserIdList().size() > 0){
            groupUserDao.groupAddUser(map);
        }
    }

    /**
     * 保存组与业务系统关联
     * @param group
     */
    private void saveGroupRelateOperation(GroupEntity group) {
        operationGroupDao.deleteOperationIdByGroupId(group.getId());
        Map map = new HashMap();
        map.put("groupId",group.getId());
        map.put("operationIdList",group.getOperationIdList());
        if(group.getOperationIdList() != null && group.getOperationIdList().size() > 0){
            operationGroupDao.saveGroupRelateOperation(map);
        }
    }

    @Override
    public void delete(Integer id) {
        groupDao.delete(id);
    }

    @Override
    public void deleteBatch(String[] ids) {
        groupDao.deleteBatch(ids);
    }

    /**
     * 批量更新组状态
     * @param ids
     * @param status 状态(0正常 1禁用)
     * @return
     */
    @Override
    public int updateBatchStatus(String[] ids, Integer status) {
        Map<String,Object> params = new HashMap<>();
        params.put("ids",ids);
        params.put("status",status);
        return groupDao.updateBatchStatus(params);
    }

    /**
     * 根据组名称查询
     * @param name
     * @return
     */
    @Override
    public List<GroupEntity> queryListByGroupName(String name) {
        return groupDao.queryListByGroupName(name);
    }
    /**
     * 验证组名称重复
     */
    @Override
    public List<GroupEntity> checkListByGroupName(Map<String, Object> map) {
        return groupDao.checkListByGroupName(map);
    }

    /**
     * 用户所属组列表
     * @param map
     * @return
     */
    @Override
    public List<GroupEntity> queryGroupListByUserId(Map<String, Object> map) {
        return groupDao.queryGroupListByUserId(map);
    }
    /**
     * 用户所属组列表总数
     * @param map
     * @return
     */
    @Override
    public int queryGroupTotalByUserId(Map<String, Object> map) {
        return groupDao.queryGroupTotalByUserId(map);
    }
}
